#!/bin/bash
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'
if [ ! -f "/etc/ssh/sshd_config" ];
then
  echo "can't fid the sshd_config file,please specify manually!"
else
  while true
    do
    dport=$(shuf -i 9000-19999 -n 1)
    echo "Please enter a port for ssh [1-65535]"
    read -p "(Default port: ${dport}):" sshport
    [ -z "$sshport" ] && sshport=${dport}
    echo $sshport
    expr ${sshport} + 1 &>/dev/null
    if [ $? -eq 0 ]; then
        if [ ${sshport} -ge 1 ] && [ ${sshport} -le 65535 ] && [ ${sshport:0:1} != 0 ]; then
            echo
            echo "---------------------------"
            echo "port = ${sshport}"
            echo "---------------------------"
            echo
            break
        fi
    fi
    echo -e "[${red}Error${plain}] Please enter a correct number [1-65535]"
    done
  sed -i "/Port[[:space:]]*[[:digit:]]/c\Port $sshport" /etc/ssh/sshd_config
  sudo service ssh restart
fi
